﻿using Bangle.BLL;
using Bangle.Model;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Customer_CustomerList : BasePage
{
    private static string FModuleCode = "003001";
    UtilityBLL ubll = new UtilityBLL();
    T_ClassificationBLL class_bll = new T_ClassificationBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        setUserControlStatus();
        if (!IsPostBack)
        {
            BindDropDownList();
        }
    }
    #endregion

    #region 检验用户权限
    private void setUserControlStatus()
    {
        //检验用户权限
        base.CheckUserPermission(FModuleCode);
    }
    #endregion

    #region 数据绑定
    private void BindDropDownList()
    {
        this.ddl_CustomerType.DataTextField = "ClassificationName";
        this.ddl_CustomerType.DataValueField = "ClassificationId";
        this.ddl_CustomerType.DataSource = class_bll.GetModelListByTypeCode("CustomerType");
        this.ddl_CustomerType.DataBind();
        this.ddl_CustomerType.Items.Insert(0, new ListItem("<全部>", ""));
    }
    #endregion

    #region Ajex获取列表数据
    [WebMethod]
    public static AjexResponseModel GetGridDataHtml(int pageSize, int pageIndex, string OrderField, string QueryString, string CustomerTypeId)
    {
        AjexResponseModel response = new AjexResponseModel();

        #region 用户权限
        if (!CheckUserPermissionOperation2(FModuleCode, "View"))
        {
            response.msg = "没有操作权限！请联系管理员";
            return response;
        }
        #endregion

        #region 组装查询条件
        string sWhere = string.Format(" CreateUserId={0} ", CurrentSession2.UserID);
        //客户类型
        if (CustomerTypeId.Trim() != "")
        {
            sWhere += string.Format(" and CustomerTypeId={0} ", CustomerTypeId);
        }

        StringBuilder sQuerySqlString = new StringBuilder();
        string[] Fields = { "CustomerName", "Country", "Email", "Phone", "CompanyName", "Skype", "Whatsapp", "QQ", "Wechat", "Webside", "Adress", "Wangwang" };
        QueryString = QueryString.Trim();
        if (QueryString.Length > 0)
        {
            string[] Values = QueryString.Split(' ');
            foreach (string vItem in Values)
            {
                if (vItem != "")
                {
                    foreach (string fItem in Fields)
                    {
                        if (sQuerySqlString.Length > 1)
                        {
                            sQuerySqlString.Append(" or ");
                        }
                        sQuerySqlString.AppendFormat("{0} like '%{1}%'", fItem, vItem);
                    }
                }
            }
            sWhere = sWhere + " and (" + sQuerySqlString.ToString() + ")";
        }
        #endregion

        #region 排序
        if (OrderField.Trim().Length > 0)
        {
            OrderField = OrderField.Trim() + ",";
        }
        #endregion

        #region 获取数据
        DataTable dt = null;
        int pCount = 0;
        try
        {
            UtilityBLL ubll = new UtilityBLL();
            dt = ubll.GetListByPage("View_T_Customer", "*", OrderField + "CreateTime", sWhere, pageSize, pageIndex, 1, out pCount);

            #region 生成HTML
            StringBuilder sHtmlString = new StringBuilder();

            string sHtmlModel = @"
                <tr {0}>
                    <td>{1}</td>
                    <td>{2}</td>
                    <td>{3}</td>
                    <td>{4}</td>
                    <td>{5}</td>
                    <td>{6}</td>
                    <td>{7}</td>
                    <td>{8}</td>
                    <td>{9}</td>
                    <td>{10}</td>
                    <td>{11}</td>
                    <td>{12}</td>
                    <td>{13}</td>
                    <td>{14}</td>
                    <td>{15}</td>
                </tr>
            ";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string sControl = "";

                #region 操作
                string sCustomerId = dt.Rows[i]["CustomerId"].ToString();
                if (CheckUserPermissionOperation2(FModuleCode, "View"))
                {
                    sControl += "&nbsp;&nbsp;<a href='javascript:void(0)' onclick=\"showWindow('客户资料查看','CustomerEdit.aspx?Action=Detail&CustomerId=" + sCustomerId + "')\" >查看</a>";
                }
                if (CheckUserPermissionOperation2(FModuleCode, "Edit"))
                {
                    sControl += "&nbsp;&nbsp;<a href='javascript:void(0)' onclick=\"showWindow('客户资料编辑','CustomerEdit.aspx?Action=Modify&CustomerId=" + sCustomerId + "')\" >编辑</a>";
                }
                if (CheckUserPermissionOperation2(FModuleCode, "Del"))
                {
                    sControl += "&nbsp;&nbsp;<a href='javascript:void(0)' onclick=\"delGridData('CustomerList.aspx/DelGridData','" + sCustomerId + "')\" >删除</a>";
                }
                if (CheckUserPermissionOperation2(FModuleCode, "View"))
                {
                    sControl += "&nbsp;&nbsp;<a href='javascript:void(0)' onclick=\"showWindow('客户进度记录','ProgressRecord.aspx?Action=Add&CustomerId=" + sCustomerId + "')\" >进度记录</a>";
                }
                #endregion

                sHtmlString.Append(string.Format(sHtmlModel
                    , (i % 2 == 0 ? "" : "class='ht-grid-even'")
                    , dt.Rows[i]["CustomerTypeName"].ToString()
                    , dt.Rows[i]["CustomerName"].ToString()
                    , BasePage.GetGenderText(dt.Rows[i]["Gender"])
                    , dt.Rows[i]["Country"].ToString()
                    , dt.Rows[i]["Email"].ToString()
                    , dt.Rows[i]["Phone"].ToString()
                    , dt.Rows[i]["CompanyName"].ToString()
                    , dt.Rows[i]["Skype"].ToString()
                    , dt.Rows[i]["Whatsapp"].ToString()
                    , dt.Rows[i]["QQ"].ToString()
                    , dt.Rows[i]["Wechat"].ToString()
                    , dt.Rows[i]["Wangwang"].ToString()
                    , dt.Rows[i]["Webside"].ToString()
                    , dt.Rows[i]["Adress"].ToString()
                    , sControl
                    ));
            }
            #endregion

            response.gridHtml = sHtmlString.ToString();
            response.recordCound = pCount;
            response.pageIndex = pageIndex;
            response.pageSize = pageSize;// BasePage.CurrentSession2.PageSize;
        }
        catch (Exception ex)
        {
            response.msg = ex.ToString();
        }
        #endregion

        return response;
    }

    [WebMethod]
    public static string DelGridData(string Id)
    {
        T_CustomerBLL bll = new T_CustomerBLL();
        string sMsg = "";

        try
        {
            bll.Delete(Guid.Parse(Id));
        }
        catch (Exception ex)
        {
            sMsg = "操作失败！";
        }
        return sMsg;
    }
    #endregion
}
